<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\AuthRequest;
use App\Models\User;
use App\Services\ExpiredService;

class AuthController extends Controller
{
    //注册登录接口
    public function login(AuthRequest $request)
    {
        $user = User::where('open_id', $request->open_id)->first();
        if (! $user) {
            $user = User::create($request->extractInputFromRules());
            if ($request->user_id) {
                //成功邀请新用户，天数+1
                $expired_service = new ExpiredService($request->user_id);
                $expired_service->addDays(1);
            }
        }

        $user->showAndUpdateApiToken();
        auth('web')->login($user);

        return \Response::success($user);
    }
}
